from django.db import models
from django.contrib.auth.models import User


"""
class User(models.Model):
    username = models.CharField(
         max_length=32,
         unique=True,
    )
    password = models.CharField(
        max_length=255,
    )

    def __str__(self):
        return self.name
"""


class Trip(models.Model):
    """
    Represents a trip to a city.

    NOTE: related_name is necessary or it will try to create a table
          with two identical attribute names (user)
    """
    owner = models.ForeignKey(User, related_name='owner', verbose_name='Owner')
    participants = models.ManyToManyField(User, related_name='participant', verbose_name='Participants')
    start_date = models.DateField()
    end_date = models.DateField()
    trip_name = models.CharField(
        max_length=255,
    )

    def __str__(self):
        return ' '.join([
          self.trip_name,
          self.owner,
        ])

class POI(models.Model):
    """
    Represents a landmark to be visited; e.g. a building, a park, a monument, etc.
    """
    poi_name = models.CharField(
         max_length=255,
         verbose_name='Name',
    )
    
    image_url = models.URLField()
    
    backlink_url = models.URLField()
    
    def has_image_url(self):
        return self.image_url is not None and len(self.image_url) > 0

    
    is_definitive = models.BooleanField(verbose_name='Mark as being definitive')

    part_of_trip = models.ForeignKey(Trip)

    location = models.CharField(
        max_length=255,
    )
    
    description = models.CharField(
        max_length=1024,
    )
    
    image_file_name = models.CharField(
        max_length=1024,
        blank=True,
    )

    is_definitive = models.BooleanField(verbose_name='Mark as being definitive')

    def __str__(self):
        return ' '.join([
          self.name,
          self.location,
          self.description,
          self.image_file_name,
          ])
   
class Vote(models.Model):
    poi  = models.ForeignKey(POI)
    user = models.ForeignKey(User)

    class Meta:
        unique_together = ('poi','user',)


class TripUser(User):
#    user = models.ForeignKey(User, related_name='twitteruser', verbose_name='Twitteruser')
#    user = models.OneToOneField(User)
    twitter_id = models.DecimalField(max_digits=20, decimal_places=0,null=True)
    access_token = models.CharField(max_length=200,null=True)
    access_token_secret = models.CharField(max_length=200,null=True)
    twitter_username = models.CharField(max_length=200,null=True)
    last_saved_poi = models.CharField(max_length=200,null=True)
    last_saved_trip = models.CharField(max_length=200,null=True)
    def __unicode__(self):
        return self.user.user.username